//
//  ASCollectionNode-Input.swift
//  soapp
//
//  Created by zhuxietong on 2020/11/16.
//

import Foundation
import Eelay
import AsyncDisplayKit

//class AvoidCollectionNode: ASCollectionNode {
//
//}
//

// MARK: - UICollectionView

public class AvoidCollectionView: ASCollectionView, UITextViewDelegate {
    override public var contentSize: CGSize {
        willSet(newValue) {
            guard !newValue.equalTo(self.contentSize) else { return }

            super.contentSize = newValue
            self.TPKeyboardAvoiding_updateContentInset()
        }
    }

    var isLoad = false
    override public var frame: CGRect {
        willSet {
            if (!isLoad){
                setup()
                isLoad = true
            }
            super.frame = frame
        }

        didSet {
            self.TPKeyboardAvoiding_updateContentInset()
        }
    }

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: layout)

        setup()
    }


    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.setup()
    }

    override public func awakeFromNib() {
        setup()
    }



    deinit {
        NotificationCenter.default.removeObserver(self)
    }

    func focusNextTextField() -> Bool {
        return self.TPKeyboardAvoiding_focusNextTextField()
    }

    @objc func scrollToActiveTextField() {
        return self.TPKeyboardAvoiding_scrollToActiveTextField()
    }

    override public func willMove(toSuperview newSuperview: UIView?) {
        super.willMove(toSuperview: newSuperview)

        guard newSuperview != nil else { return }
        NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(TPKeyboardAvoiding_assignTextDelegateForViewsBeneathView(_:)), object: self)
    }

    override public func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.TPKeyboardAvoiding_findFirstResponderBeneathView(self)?.resignFirstResponder()
        super.touchesEnded(touches, with: event)
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        if !self.focusNextTextField() {
            textField.resignFirstResponder()
        }
        return true
    }

    override public func layoutSubviews() {
        super.layoutSubviews()

        NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(TPKeyboardAvoiding_assignTextDelegateForViewsBeneathView(_:)), object: self)

        Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(TPKeyboardAvoiding_assignTextDelegateForViewsBeneathView(_:)), userInfo: nil, repeats: false)
    }
}

private extension AvoidCollectionView {
    func setup() {
        NotificationCenter.default.addObserver(self, selector: #selector(TPKeyboardAvoiding_keyboardWillShow(_:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)

        NotificationCenter.default.addObserver(self, selector: #selector(TPKeyboardAvoiding_keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)

        NotificationCenter.default.addObserver(self, selector: #selector(scrollToActiveTextField), name: UITextView.textDidBeginEditingNotification, object: nil)

        NotificationCenter.default.addObserver(self, selector: #selector(scrollToActiveTextField), name: UITextField.textDidBeginEditingNotification, object: nil)
    }
}




// MARK: - UICollectionView
//
//public class AvoidCollectionView: ASCollectionView, UITextViewDelegate {
//    override public var contentSize: CGSize {
//        willSet(newValue) {
//            guard !newValue.equalTo(self.contentSize) else { return }
//
//            super.contentSize = newValue
//            self.TPKeyboardAvoiding_updateContentInset()
//        }
//    }
//
//    override public var frame: CGRect {
//        willSet {
//            super.frame = frame
//        }
//
//        didSet {
//            self.TPKeyboardAvoiding_updateContentInset()
//        }
//    }
//
//    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
//        super.init(frame: frame, collectionViewLayout: layout)
//        setup()
//    }
//
//    required init?(coder aDecoder: NSCoder) {
//        super.init(coder: aDecoder)
//        self.setup()
//    }
//
//    override public func awakeFromNib() {
//        setup()
//    }
//
//    deinit {
//        NotificationCenter.default.removeObserver(self)
//    }
//
//    func focusNextTextField() -> Bool {
//        return self.TPKeyboardAvoiding_focusNextTextField()
//    }
//
//    @objc func scrollToActiveTextField() {
//        return self.TPKeyboardAvoiding_scrollToActiveTextField()
//    }
//
//    override public func willMove(toSuperview newSuperview: UIView?) {
//        super.willMove(toSuperview: newSuperview)
//
//        guard newSuperview != nil else { return }
//        NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(TPKeyboardAvoiding_assignTextDelegateForViewsBeneathView(_:)), object: self)
//    }
//
//    override public func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
//        self.TPKeyboardAvoiding_findFirstResponderBeneathView(self)?.resignFirstResponder()
//        super.touchesEnded(touches, with: event)
//    }
//
//    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
//        if !self.focusNextTextField() {
//            textField.resignFirstResponder()
//        }
//        return true
//    }
//
//    override public func layoutSubviews() {
//        super.layoutSubviews()
//
//        NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(TPKeyboardAvoiding_assignTextDelegateForViewsBeneathView(_:)), object: self)
//
//        Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(TPKeyboardAvoiding_assignTextDelegateForViewsBeneathView(_:)), userInfo: nil, repeats: false)
//    }
//}
//
//private extension AvoidCollectionView {
//    func setup() {
//        NotificationCenter.default.addObserver(self, selector: #selector(TPKeyboardAvoiding_keyboardWillShow(_:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
//
//        NotificationCenter.default.addObserver(self, selector: #selector(TPKeyboardAvoiding_keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
//
//        NotificationCenter.default.addObserver(self, selector: #selector(scrollToActiveTextField), name: UITextView.textDidBeginEditingNotification, object: nil)
//
//        NotificationCenter.default.addObserver(self, selector: #selector(scrollToActiveTextField), name: UITextField.textDidBeginEditingNotification, object: nil)
//    }
//}
//
